package com.shawn.springboot.integeration.system.service.impl;

import com.shawn.springboot.integeration.system.model.pojo.SysUser;
import com.shawn.springboot.integeration.system.service.ISysPasswordService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;

/**
 * @author shawn
 * @description: 密码校验服务实现
 * @date 2022/9/1
 */
@Service
public class SysPasswordServiceImpl implements ISysPasswordService {

    @Override
    public void validate(SysUser sysUser, String password) {
        if (!matches(sysUser, password)) {
            throw new RuntimeException("密码校验失败");
        }
    }

    @Override
    public String encryptPassword(String password, String salt) {
        return new Md5Hash(password, salt, 1).toHex();
    }

    private boolean matches(SysUser user, String newPassword) {
        return user.getPassword().equals(encryptPassword(newPassword, user.getSalt()));
    }

    public static void main(String[] args) {
        String password = "ry";
        String salt = "222222";
        Md5Hash md5Hash = new Md5Hash(password, salt, 1);
        System.out.println(md5Hash.toHex());
    }
}
